/**
 * XGPHP 轻量级PHP框架
 * @link http://xgphp.xg3.cn
 * @version 2.0.0
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @author 讯高科技 <xungaokeji@qq.com>
*/
(function($){
xg.def('tab',function(){
	var T=function(name){
		var s=this;
		window.xg_tab_list=window.xg_tab_list||{};
		window.xg_tab_list[name]=s;
		s.o={};
		s.tabs={};
	}
	T.prototype.id=0;
	T.prototype.add=function(title,content,id){
		var s=this,id=id||s.id;
		if(s.tabs[id]){
			s.setTitle(id,title);
			s.setContent(id,content);
			return s.tabs[id];
		}
		const $title=$('<li xg-id="'+id+'"></li>').append(title);//.append('<div class="xg-tab-title-close"><a class="xg-close"></a></div>');
		const $content=$('<li xg-id="'+id+'"></li>').append(content);
		s.title.append($title);
		s.content.append($content);
		bind(s,id);
		if(!isNaN(parseInt(id)) && s.id <= id){
			s.id=id+1;
		}
		s.more();
		if(xg.isfun(s.o.add)){
			s.o.add(id,s.title.children('[xg-id="'+id+'"]'),s.content.children('[xg-id="'+id+'"]'));
		}
		s.tabs[id]={
			title:$title,
			content:$content,
			id:id
		};
		return s.tabs[id];
	}
	T.prototype.setTitle=function(id,title){
		var s=this;
		s.title.children('[xg-id="'+id+'"]').empty().append(title);//.append('<div class="xg-tab-title-close"><a class="xg-close"></a></div>');
		return s;
	}
	T.prototype.setContent=function(id,content){
		var s=this;
		s.content.children('[xg-id="'+id+'"]').empty().append(content);
		return s;
	}
	T.prototype.option=function(o,v){
		var s=this;
		if(xg.isobj(o)){
			s.o=$.extend({},s.o,o);
		}else{
			s.o[o]=v;
		}
		return s;
	}
	T.prototype.onshow=function(f){
		var s=this;
		s.o.show=f;
		return s;
	}
	T.prototype.more=function(){
		var s=this;
		if(s.o.more!==false){
			if(xg.isfun(xg.device) && xg.device().mobile){
				s.title.css({overflowX:'auto'});
			}else{
				s.title.css({overflowX:'none'});
				if(getmore(s).length==0){
					s.morebtn.hide();
					if(s.morepop)s.morepop.remove();
				}else{
					s.morebtn.show();
				}
			}
			if(xg.isfun(s.o.more)){
				s.o.more(getmore(s).length>0);
			}
		}
	}
	T.prototype.clear=function(){
		var s=this;
		s.title.children('[xg-id]').each(function(){
			s.remove($(this).attr('xg-id'));
		});
		return s;
	}
	T.prototype.remove=function(id){
		var s=this;
		var isthis=s.title.children('[xg-id="'+id+'"]').hasClass('xg-this');
		s.title.children('[xg-id="'+id+'"]').remove();
		s.content.children('[xg-id="'+id+'"]').remove();
		if(isthis)s.show(s.title.children('li').last().attr('xg-id'));
		if(xg.isfun(s.o.remove))s.o.remove(id,isthis);
		s.more();
		return s;
	}
	T.prototype.show=function(id){
		var s=this;
		if(xg.isunde(id)){
			if(s.title.children('li.xg-this').length>0){
				return;
			}
			id=s.title.children('[xg-id]').first().attr('xg-id');
		}
		if(id)s.events[id].show();
		s.title.children('li').removeClass('xg-this').filter('[xg-id='+id+']').addClass('xg-this');
		s.content.children('li').removeClass('xg-this').filter('[xg-id='+id+']').addClass('xg-this');
		if(xg.isfun(s.o.show))s.o.show(id,s.title.children('[xg-id="'+id+'"]'),s.content.children('[xg-id="'+id+'"]'));
		// xg.hooks.run('tab-show',id,s.title.children('[xg-id="'+id+'"]'),s.content.children('[xg-id="'+id+'"]'));
		return s;
	}
	T.prototype.iframe=function(id,t){
		var s=this;
		var iframe=s.content.children('li[xg-id="'+id+'"]').find('iframe');
		if(iframe.length){
			if(t==1)return iframe;
			if(t==2)return iframe[0];
			return iframe[0].contentWindow;
		}
	}
	T.prototype.events=[];
	T.prototype.event=function(id){
		var s=this,e=s.events[id];
		if(!e){
			var e=new event();
			s.events[id]=e;
		}
		return e;
	}
	T.prototype.bind=function(title,content,more){
		var s=this;
		s.title=$(title);
		s.content=$(content);
		s.morebtn=$(more);
		s.show();
		s.title.children('li').each(function(){
			if(!$(this).attr('xg-id')){
				$(this).attr('xg-id',$(this).index()+1)
			}
		});
		s.content.children('li').each(function(){
			if(!$(this).attr('xg-id')){
				$(this).attr('xg-id',$(this).index()+1)
			}
		});
		s.title.children('li').each(function(){
			bind(s,$(this).attr('xg-id'));
			s.id=xg.int($(this).attr('xg-id'))+1;
		});
		s.morebtn.click(function(){
			var more=getmore(s);
			s.morepop=xg.pop().ref($(this),more,{alignleft:1,alignbottom:1,class:'xg-tab-more-title'}).up();
		});
		return s;
	}
	function event(){}
	event.prototype.events={show:[]};
	event.prototype.onshow=function(f){
		this.events.show.push(f);
	}
	event.prototype.show=function(){
		for(var id in this.events.show){
			this.events.show[id]();
		}
	}
	function getmore(s){
		var more=$();
		s.title.children('li').each(function(){
			if($(this).offset().left+$(this).width()>s.title.offset().left+s.title.width()){
				more.push($(this).clone(true)[0]);
			}
		});
		more.find('.xg-close').click(function(e){
			e.stopPropagation();
			var id=$(this).parent().attr('xg-id');
			s.remove(id);
			$(this).closest('li').remove();
			//s.morepop.remove();
		});
		if(more.length){
			more=xg.div('xg-tab-title xg-tab-show-close').append(more);
		}
		return more;
	}
	function bind(s,id){
		s.event(id);
		s.title.children('[xg-id="'+id+'"]').mousedown(function(e){
			e.tabid=id;
			e.tabtit=s.title.children('[xg-id="'+id+'"]');
			e.tabcont=s.content.children('[xg-id="'+id+'"]');
			if((e.button===0||e.button===1)&&xg.isfun(s.o.mouse))var rt=s.o.mouse(e);
			e.preventDefault();
		}).contextmenu(function(e){
			e=e||{};
			e.button=2;
			e.tabid=id;
			e.tabtit=s.title.children('[xg-id="'+id+'"]');
			e.tabcont=s.content.children('[xg-id="'+id+'"]');
			setTimeout(function(){if(s.morepop)s.morepop.remove();},10);
			if(xg.isfun(s.o.mouse))return s.o.mouse(e);
		}).click(function(e){
			s.show(id);
			setTimeout(function(){if(s.morepop)s.morepop.remove();},10);
			e=e||{};
			e.button=0;
			e.tabid=id;
			e.tabtit=s.title.children('[xg-id="'+id+'"]');
			e.tabcont=s.content.children('[xg-id="'+id+'"]');
			if(xg.isfun(s.o.mouse))return s.o.mouse(e);
		}).dblclick(function(e){
			e=e||{};
			e.button=3;
			e.tabid=id;
			e.tabtit=s.title.children('[xg-id="'+id+'"]');
			e.tabcont=s.content.children('[xg-id="'+id+'"]');
			if(xg.isfun(s.o.mouse))return s.o.mouse(e);
		}).find('.xg-close').click(function(){
			s.remove(id);
			s.more();
		});
	}
	return function(name){
		if(window.xg_tab_list&&window.xg_tab_list[name])return window.xg_tab_list[name];
		return new T(name);
	};
});
xg.tab.get=function(name){
	if(!window.xg_tab_list)return;
	var s=window.xg_tab_list[name];
	if(!s)xg.error('没有此名称的标签卡');
	return s;
}
})(jQuery);